package com.czl.module_service.viewmodel.assetUse

import com.blankj.utilcode.util.GsonUtils
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.tengyun.ListBean
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

class PurchaseSortListViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var purchaseId : Int? = null
    val uc = UiChangeEvent()

    class UiChangeEvent {
        val loadCompleteEvent: SingleLiveEvent<MutableList<SortBean>> = SingleLiveEvent()
    }

    val onLoadMoreListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getPurchaseStorageSortList(purchaseId = purchaseId)
    })

    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getPurchaseStorageSortList(purchaseId = purchaseId)
    })

    fun getPurchaseStorageSortList(
        purchaseId: Int? = null
    ) {
        model.mTengYunHttpDataSource.getPurchaseStorageSortList(
            GsonUtils.toJson(
                mapOf(
                    "purchaseId" to purchaseId
                )
            )
        )
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriberHelper<BaseBean<ListBean<SortBean>>>() {
                override fun onResult(t: BaseBean<ListBean<SortBean>>) {
                    if (t.state == 0) {
                        uc.loadCompleteEvent.postValue(t.data?.list as MutableList<SortBean>?)
                    }
                    dismissLoading()
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }
            })
    }
}